package cn.est.controller;

import cn.est.config.OssConfig;
import cn.est.dto.Result;
import cn.est.utils.FileUtils;
import cn.est.utils.OssUtils;
import cn.est.utils.ResultUtils;
import com.aliyun.oss.OSS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Api("file")
@RestController
@RequestMapping("/api/file")
public class FileController {

    @Autowired
    private OssUtils ossUtils;

    @Autowired
    private OssConfig ossConfig;

    @ApiOperation(value = "文件上传",notes = "单文件上传")
    @PostMapping("/uploadfile")
    public Result uploadFile(MultipartFile file){
        String fileName=file.getOriginalFilename();
        String filePath="est/model/";
        File fileInfo= FileUtils.MultipartFileToFile(file);
        String result=ossUtils.uploadByStream(fileInfo,filePath+fileName);
        return ResultUtils.returnDataSuccess(ossConfig.getOssWebUrl()+filePath+fileName);
    }

    @ApiOperation(value = "文件删除",notes = "单文件删除")
    @DeleteMapping("/deletefile")
    public Result deleteFile(String filePath,String fileName){
        ossUtils.deleteFile(ossConfig.getBucketName(),filePath+fileName);
        return ResultUtils.returnSuccess("删除文件成功");
    }



}
